package com.cy.service.impl;

import com.cy.anno.Transactional;
import com.cy.utils.TransactionManager;
import com.cy.anno.Autowired;
import com.cy.anno.Component;
import com.cy.dao.AccountDao;
import com.cy.pojo.Account;
import com.cy.service.TransferService;

/**
 * @author 应癫
 */
@Component
public class TransferServiceImpl implements TransferService {

//    private AccountDao accountDao = (AccountDao) BeanFactory.getInstance().getBean("accountDao");

    @Autowired
    private AccountDao accountDao;

    @Autowired
    private TransactionManager transactionManager;

    @Override
    @Transactional
    public void transfer(String fromCardNo, String toCardNo, int money) throws Exception {
//        try {
//            transactionManager.beginTransaction();

            Account from = accountDao.queryAccountByCardNo(fromCardNo);
            Account to = accountDao.queryAccountByCardNo(toCardNo);

            from.setMoney(from.getMoney()-money);
            to.setMoney(to.getMoney()+money);

            accountDao.updateAccountByCardNo(to);
            //制造错误
            int i = 1/0;
            accountDao.updateAccountByCardNo(from);

//            transactionManager.commit();
//        }catch (Exception e){
//            e.printStackTrace();
//            transactionManager.rollback();
//            throw e;
//        }

    }
}
